﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class ApproveRequisition : System.Web.UI.Page
   
    {
        ApproveRequisitionController approveController = new ApproveRequisitionController();
        //String userName = "CPSC_Head";  //to change hard code
        String userName;
        //int roleID;

        protected void Page_Load(object sender, EventArgs e)
        {
            try{
            if (Session["UserLoggedIn"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                List<String> loginValues = (List<String>)(Session["UserLoggedIn"]);
                if (loginValues[6] == "4" || loginValues[6] == "7") //check whether login user's role is equal to Dept Head or store manager
                {
                    userName = loginValues[3];
                }
                else
                {
                    Response.Redirect("login.aspx");
                }

            }
            if (!IsPostBack)
            {
                
                ddlEmployee.DataSource = approveController.getPendingEmpList(userName);
                ddlEmployee.DataTextField = "EmployeeName";
                ddlEmployee.DataValueField = "EmployeeID";
                ddlEmployee.DataBind();
                ddlEmployee.Items.Insert(0, "All");
                gvRequisition.DataSource = approveController.getAllPendingRequisition(userName);
                gvRequisition.DataBind();

            }
            }
            catch(Exception ex)
            {

            }
            

        }

        protected void gvRequisition_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int rowIndex = Convert.ToInt32(e.CommandArgument);
                String reqID = gvRequisition.Rows[rowIndex].Cells[0].Text;
                String empID = gvRequisition.Rows[rowIndex].Cells[1].Text;
                Response.Redirect("~/ViewDetailOfRequisition.aspx?reqID=" + reqID + "&empID=" + empID);

            }
        }

        protected void ddlEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlEmployee.SelectedItem.Text == "All")
            {
                gvRequisition.DataSource = approveController.getAllPendingRequisition(userName);
                gvRequisition.DataBind();
            }
            else
            {
                int empId = Convert.ToInt32(ddlEmployee.SelectedValue.ToString());
                gvRequisition.DataSource = approveController.getPendingReq(empId);
                gvRequisition.DataBind();
            }

        }
    }
}